package com.example.wiflyproto;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends Activity {
	private Button connect, refresh;
	private TextView tv;
	private EditText et;
	private Socket socket;
	private String serverIpAddress = "";
	private InetAddress serverAddr;
	private String line ="", st="";


	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		connect = (Button) findViewById(R.id.connect);
		refresh = (Button) findViewById(R.id.refresh);
		tv = (TextView) findViewById(R.id.myTextView);
		et = (EditText) findViewById(R.id.IPText01);
		
		et.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) 
			{
				et.setText("");		
			}
		});

		connect.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					serverIpAddress = ((EditText) findViewById(R.id.IPText01)).getText().toString();

					serverAddr = InetAddress.getByName(serverIpAddress);
					socket = new Socket(serverAddr, 80);
					

					PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
					out.println("");
					
					Log.d("Client", "Client sent message");
										
					BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					
					st = "";
					while((line =input.readLine()) != null)
					{
						st += line+"\n";
					}
	                
	                tv.setText(st);
			
				} catch (UnknownHostException e) {
					tv.setText("Error1");
					e.printStackTrace();
				} catch (IOException e) {
					tv.setText("Error2");
					e.printStackTrace();
				} catch (Exception e) {
					tv.setText("Error3");
					e.printStackTrace();
				}
			}
		});
		
		refresh.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				try {
					
					socket = new Socket(serverAddr, 80);
					

					PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
					out.println("");
					
					Log.d("Client", "Client sent message");
										
					BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					
					st = "";
					while((line =input.readLine()) != null)
					{
						st += line+"\n";
					}
	                
	                tv.setText(st);
			
				} catch (UnknownHostException e) {
					tv.setText("Error1");
					e.printStackTrace();
				} catch (IOException e) {
					tv.setText("Error2");
					e.printStackTrace();
				} catch (Exception e) {
					tv.setText("Error3");
					e.printStackTrace();
				}
			}
		});
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		try {
			socket.close();
		} catch (IOException e) {
			tv.setText("socket close error");
			e.printStackTrace();
		}
	}
}